package com.hxzy.controller;

import com.hxzy.common.domain.AjaxResult;
import com.hxzy.common.utils.StringUtils;
import com.hxzy.dto.MenuSearchDTO;
import com.hxzy.entity.AdminUser;
import com.hxzy.entity.Menu;
import com.hxzy.framework.controller.BaseController;
import com.hxzy.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.util.Date;
import java.util.List;

/**
 * 菜单类说明
 *
 * @author admin
 * @date 2021-09-14
 */
@Controller
@RequestMapping(value = "/admin")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    /**
     * 显示页面
     * @return
     */
    @RequiresPermissions(value = "system:menu:view")
    @GetMapping(value = "/menu/list")
    public String showPage(){
        return "system/menu/list";
    }

    /**
     * 显示菜单数据
     * @return
     */
    @RequiresPermissions(value = "system:menu:list")
    @ResponseBody
    @PostMapping(value = "/menu/list")
    public List<Menu> ajaxData(MenuSearchDTO menuSearchDTO){

        List<Menu> menuList = this.menuService.selectAllMenuInfo(menuSearchDTO);

        return menuList;
    }


    /**
     * 显示新增页面
     * @param model
     * @return
     */
    @GetMapping(value = "/menu/add/{id}")
    public String addPage(@PathVariable(value = "id") Long id, Model model){
        //新增主目录
       if(id.equals(0L)){
           model.addAttribute("menuType","M");
           model.addAttribute("parentId",0);
           model.addAttribute("parentName","主目录");
           model.addAttribute("menuName","目录名称");
       }else{
           //不为0  要么是目录，要么是 菜单
           Menu  menu=this.menuService.selectByPrimaryKey(id);
           if(menu==null){
               return "error/404";
           }
           //如果menu.MenuType是M那新增的一定是菜单C，  如果menu.MenuType是C那么新增的一定是按钮F
           model.addAttribute("menuType", menu.getMenuType().equals("M")?"C":"F");
           model.addAttribute("parentId",id);
           model.addAttribute("parentName",menu.getName());
           model.addAttribute("menuName",menu.getMenuType().equals("M")?"菜单名称":"按钮名称");
       }
       return "system/menu/add";
    }

    @RequiresPermissions(value = "system:menu:add")
    @ResponseBody
    @PostMapping(value = "/menu/add")
    public AjaxResult addData(Menu menu){
        AdminUser adminUser=(AdminUser) SecurityUtils.getSubject().getPrincipal();
        menu.setCreateTime(new Date());
        menu.setUpdateTime(new Date());
        menu.setCreateBy(adminUser.getLoginUserName());
        menu.setUpdateBy(adminUser.getLoginUserName());

        if(StringUtils.isBlank(menu.getIcon())){
            menu.setIcon("#");
        }
        //按钮 菜单状态=0，是否刷新=1   url=#  target=menuItem
        if(menu.getMenuType().equals("F")){
            menu.setRefresh("1");
            menu.setVisible("0");
            menu.setTarget("menuItem");
            menu.setUrl("#");
        }else if(menu.getMenuType().equals("M")){
            //如果是目录  refresh=1  url=#
            menu.setRefresh("1");
            menu.setUrl("#");
            menu.setTarget("menuItem");
        }

        int count = this.menuService.insert(menu);
        return super.toAjax(count);
    }


    /**
     * 显示修改页面
     * @param model
     * @return
     */
    @RequiresPermissions(value = "system:menu:edit")
    @GetMapping(value = "/menu/edit/{id}")
    public String editPage(@PathVariable(value = "id") Long id, Model model){
        if(id==null && id.intValue()<=0){
            return "error/404";
        }

        Menu  menu=this.menuService.selectByPrimaryKey(id);
        if(menu==null){
            return "error/404";
        }

        model.addAttribute("menuType",menu.getMenuType());
        model.addAttribute("parentId",menu.getParentId());
        //修改主目录
        if(menu.getParentId().equals(0L)){
            model.addAttribute("parentName","主目录");
            model.addAttribute("menuName","目录名称");
        }else{
            //查询它的上级是谁 
            Menu parentMenu = this.menuService.selectByPrimaryKey(menu.getParentId());

            model.addAttribute("parentName",parentMenu.getName());
            model.addAttribute("menuName",menu.getMenuType().equals("C")?"菜单名称":"按钮名称");
        }

         model.addAttribute("menu",menu);
        return "system/menu/edit";
    }


    @RequiresPermissions(value = "system:menu:edit")
    @ResponseBody
    @PostMapping(value = "/menu/edit")
    public AjaxResult editData(Menu menu){
        AdminUser adminUser=(AdminUser) SecurityUtils.getSubject().getPrincipal();
        menu.setUpdateTime(new Date());
        menu.setUpdateBy(adminUser.getLoginUserName());

        if(StringUtils.isBlank(menu.getIcon())){
            menu.setIcon("#");
        }

        int count = this.menuService.updateByPrimaryKeySelective(menu);
        return super.toAjax(count);
    }


}
